module aps_top 
   implicit none 
   integer ::ptr,ptr2
   integer :: k,j,window,ty,g
   integer :: cw_hndl 
   integer :: met1(3)
   integer :: met2(2)
   integer :: u(9),type_j(3)
   real*8 :: number1,number2
   common number1, number2
   character*256 :: str
   character*256 :: name
   data u/1,0,0,0,0,0,0,0,0/
   data met1/1,0,0/
   data met2/1,0/
   data type_j/0,1,0/
  
contains 

	integer function test()
      use mswin
	implicit none
	INTEGER COPY
	CHARACTER BUFFER
    call get_clearwin_text@(1,BUFFER,COPY)
    if (COPY .EQ. 0) then
      window = winio@('NO')
    else
      window = winio@('YES')
    end if
    test=1
    end function test
      
   integer function cbupdate() 
      use mswin 
      call see_propertysheet_page@(ptr) 
      cbupdate=2 
   end function cbupdate
    
   integer function cbdummy() 
      use mswin
      implicit none
      if(u(1) /= 0) then
          write(*,'(A)') 'f(x) = (x((2.1-0.5x)^0.5)/((1-x)((1.1-0.5x)^0.5)))-3.69'
          call display3method() 
      else if (u(2)/= 0) then
          write(*,'(A)') 'f(x) = tan(x)-x+1'
          call display3method()
      else if (u(3)/= 0) then
          write(*,'(A)') 'f(x) = 0.5(e^x/3)-sin x'
          call display3method()
      else if (u(4)/= 0) then
          write(*,'(A)') '3x^2-2y^2-1 =0'
          write(*,'(A)') 'x^2-2x+y^2+2y-8=0'
          call display22method()
      else if (u(5)/= 0) then
          write(*,'(A)') 'f(t) = 80(e^-2t)+20(e^-0.1t)-10'
          call display2method()
      else if (u(6)/= 0) then
          write(*,'(A)') 'f(R) = (R+1/R(1+0.01R))-ln((1+0.01R)/0.01R)'
          call display3method()
      else if (u(7)/= 0) then
          write(*,'(A)') 'f(R) = (R+1/R(1+0.005R))-ln((1+0.005R)/0.005R)'
          call display3method()
      else if (u(8)/= 0) then
          write(*,'(A)') 'f(R) = (R+1/R(1+0.001R))-ln((1+0.001R)/0.001R)'
          call display3method()
      else if (u(9)/=0) then
          write(*,'(A)') 'f(v) = 1.4x10^-5 v^1.5 + 1.15x10^-5v^2'  
          call display3method()    
      end if
      call window_update@(str)
      cbdummy=1 
   end function cbdummy 
   
   integer function cb_clear() 
      use mswin 
      implicit none 
      call clear_window@(0) 
      cb_clear=1 
   end function cb_clear 
   
	subroutine read_data_file()
    implicit none
    character(80) :: line
    integer reason
    character n
    open(unit=10,file='temp.dat',status='old',action='read')
    DO
   	READ(10,'(A)',IOSTAT=Reason) line
   	IF (Reason > 0)  THEN
      write(*,*) 'error in reading file'
      exit
   	ELSE IF (Reason < 0) THEN
       exit
   	ELSE
		write(*,*) line
   	END IF
	END DO
	close(10)
    end subroutine read_data_file

   subroutine display3method()
   implicit none
   number1 = 0
   number2 = 0
          window = winio@ ('%ca[Pilih Metode dan Masukkan Selang Hasil]&')
          window = winio@ ('%bg[btnface]&')
          window = winio@ ('%ob[raised]&')
          window = winio@ ('Pilih Metode&')
          window = winio@ ('%3`ga&',met1(1),met1(2),met1(3))
          window = winio@ ('%2nl%rb[Metode Bagi Dua]&',met1(1))
          window = winio@ ('%2nl%rb[Metode Regula Falsi]&',met1(2))
          window = winio@ ('%2nl%rb[Metode Secant]%2nl&',met1(3))
          window = winio@ ('%nl%lw%cb&',-1)
          window = winio@ ('%ob[raised]&')
          window = winio@ ('%2nlMasukkan selang hasil&')
          window = winio@ ('%fl&',-10000000000.0D0,10000000000.0D0)
          window = winio@ ('%2nlNilai selang pertama  : %rf&',number1)
          window = winio@ ('%2nlNilai selang kedua : %rf&',number2)
          window = winio@ ('%nl%cb&')
          window = winio@ ('%2nl%^bt[OK]&',solve)
          window = winio@ ('%2nl%bt[Cancel]')
   end subroutine display3method
   
   subroutine display2method()
   implicit none
   number1 = 0
        window = winio@('%ca[Pilih Metode dan Masukkan Angka Tebakan]&')
        window = winio@('%bg[btnface]&')
        window = winio@('%ob[raised]&')
        window = winio@('Pilih Metode&')
        window = winio@('%2`ga&',met2(1),met2(2))
        window = winio@('%2nl%rb[Metode lelaran Jacobi]%2nl&',met2(1))
        window = winio@('%2nl%rb[Metode Newton-Raphson]%2nl&',met2(2))
        window = winio@('%nl%lw%cb&',-1)
        window = winio@('%ob[raised]&')
        window = winio@('%2nlMasukkan angka tebakan&')
        window = winio@('%fl&',-1000.0D0,1000.0D0)
        window = winio@('%2nlNilai tebakan  : %rf&',number1)
        window = winio@('%nl%cb&')
        window = winio@('%2nl%^bt[OK]&',solve)
        window = winio@('%2nl%bt[Cancel]')
   end subroutine display2method
   
	subroutine display22method()
   implicit none
   number1 = 0
   number2=0
        window = winio@('%ca[Pilih Metode dan Masukkan Angka Tebakan]&')
        window = winio@('%bg[btnface]&')
        window = winio@('%ob[raised]&')
        window = winio@('Pilih Metode&')
        window = winio@('%2`ga&',met2(1),met2(2))
        window = winio@('%2nl%rb[Metode lelaran Jacobi]%2nl&',met2(1))
        window = winio@('%2nl%rb[Metode Newton-Raphson]%2nl&',met2(2))
        window = winio@('%nl%lw%cb&',-1)
        window = winio@('%ob[raised]&')
        window = winio@('%2nlMasukkan angka tebakan&')
        window = winio@('%fl&',-1000.0D0,1000.0D0)
        window = winio@('%2nlNilai tebakan 1 : %rf&',number1)
        window = winio@('%2nlNilai tebakan 2 : %rf&',number2)
        window = winio@('%nl%cb&')
        window = winio@('%ob[raised]&')
        window = winio@('Pilih Persamaan (khusus jacobi)&')
        window = winio@('%3`ga&',type_j(1),type_j(2),type_j(3))
        window = winio@('%2nl%rb[persamaan 1]%2nl&',type_j(1))
        window = winio@('%2nl%rb[persamaan 2]%2nl&',type_j(2))
        window = winio@('%2nl%rb[persamaan 3]%2nl&',type_j(3))
        window = winio@('%nl%cb&')
        window = winio@('%2nl%^bt[OK]&',solve)
        window = winio@('%2nl%bt[Cancel]')
   end subroutine display22method

	integer function cb_save()
    implicit none
    window=winio@('%ca[Masukan Nama File]&')
    window=winio@('%bg[btnface]&')
    window=winio@('%42rs&',name) 
    window=winio@('%ta%^bt[OK]&',save_f)
    window=winio@('%ta%2nl%bt[Cancel]')
	cb_save = 1
    end function cb_save

	integer function save_f()
    character(80) :: line
    integer reason
    character n
    open(11,FILE=name)
    open(unit=10,file='temp.dat',status='old',action='read')
    DO
   	READ(10,'(A)',IOSTAT=Reason) line
   	IF (Reason > 0)  THEN
      write(*,*) 'error in reading file'
      exit
   	ELSE IF (Reason < 0) THEN
       exit
   	ELSE
    	write(11,'(A)') line
   	END IF
	END DO
	close(10)
    close(11)
    save_f = -1
    end function save_f

	integer function cb_print()
    implicit none
    character(80) :: line
    integer reason
    character n
    open(unit=10,file='temp.dat',status='unknown',action='read')
    DO
   	READ(10,'(A)',IOSTAT=Reason) line
   	IF (Reason > 0)  THEN
      write(7,*) 'error in reading file'
      exit
   	ELSE IF (Reason < 0) THEN
       exit
   	ELSE
		write(7,*) line
   	END IF
	END DO
	close(10)
    close(7)
    cb_print = 2
    end function cb_print

   integer function solve()
   use graph
   use formula
   implicit none
   real*8 res,eps,error
   common number1,number2
   integer*2 count,maxcount
   maxcount = 100
   open(10,file='temp.dat')
   
   if (u(1) /= 0 .AND. met1(1) /= 0) then
   g=1
   eps = 0.00001
   call bagidua (eps,number1,number2,res,count,1)
   if (maxcount > count) then
    write (10,*) 'Jawaban adalah ',res
   else
    write (10,*) 'Max count reached'
   end if
   
   else if (u(1) /= 0 .AND. met1(2) /= 0) then
     g=1
   eps = 0.00001
   call regulafalsi (eps,number1,number2,res,count,1)
   if (maxcount > count) then
    write (10,*) 'Jawaban adalah ',res
   else
    write (10,*) 'Max count reached'
   end if
   
   else if (u(1) /= 0 .AND. met1(3) /=0) then
     g=1
   eps = 0.00001
   call secant (eps,number1,number2,res,count,1)
   if (maxcount > count) then
    write (10,*) 'Jawaban adalah ', res
   else
    write (10,*) 'Max count reached'
   end if
   
   else if (u(2) /= 0 .AND. met1(1) /= 0) then
     g=2
   eps = 0.00001
   call bagidua (eps,number1,number2,res,count,2)
   if (maxcount > count) then
    write (10,*) 'Jawaban adalah ',res
   else
    write (10,*) 'Max count reached'
   end if
   
   else if (u(2) /= 0 .AND. met1(2) /= 0) then
     g=2
   eps = 0.00001
   call regulafalsi (eps,number1,number2,res,count,2)
   if (maxcount > count) then
    write (10,*) 'Jawaban adalah ',res
   else
    write (10,*) 'Max count reached'
   end if
   
   else if (u(2) /= 0 .AND. met1(3) /= 0) then
     g=2
   eps = 0.00001
   call secant (eps,number1,number2,res,count,2)
   if (maxcount > count) then
    write (10,*) 'Jawaban adalah ',res
   else
    write (10,*) 'Max count reached'
   end if
      
   else if (u(3) /= 0 .AND. met1(1) /= 0) then
     g=3
   eps = 0.00001
   call bagidua (eps,number1,number2,res,count,3)
   if (maxcount > count) then
    write (10,*) 'Jawaban adalah ',res
   else
    write (10,*) 'Max count reached'
   end if
   
   else if (u(3) /= 0 .AND. met1(2) /= 0) then
     g=3
   eps = 0.00001
   call regulafalsi (eps,number1,number2,res,count,3)
   if (maxcount > count) then
    write (10,*) 'Jawaban adalah ',res
   else
    write (10,*) 'Max count reached'
   end if
   
   else if (u(3) /= 0 .AND. met1(3) /= 0) then
     g=3
   eps = 0.00001
   call secant (eps,number1,number2,res,count,3)
   if (maxcount > count) then
    write (10,*) 'Jawaban adalah ',res
   else
    write (10,*) 'Max count reached'
   end if
   
   else if (u(4) /= 0 .AND. met2(1) /= 0) then
     g=4
   	eps = 0.000001
    if(type_j(1) /= 0) then  
	    ty = 1
    else
      	ty = 2
    end if
   	call Jacobi(eps,number1,number2,count,ty)
   	if(maxcount > count) then
   		write(10,*) 'Jawaban adalah ',number1,",",number2
   	else
   		write(10,*) 'Max count reached'  
   	end if
   	
   else if (u(4) /= 0 .AND. met2(2) /= 0) then
     g=4
   eps = 0.000001
   	call Lelaran_NR(eps,number1,number2,count)
   	if(maxcount > count) then
         write(10,*) 'Jawaban adalah ',number1,",",number2
   	else
   		write(10,*) 'Max count reached'  
   	end if
   	
   else if (u(5) /= 0 .AND. met2(1) /= 0) then
     g=0
   window = winio@('Persoalan tidak diselesaikan dengan metode ini')
   
   else if (u(5) /= 0 .AND. met2(2) /= 0) then
     g=5
   	eps = 0.000001
    call Newton_Raphson(eps,number1,res,error,count,maxcount)
   	if(maxcount > count) then
   		write(10,*) 'Jawaban adalah',res
   	else
   		write(10,*) 'Max count reached'  
   	end if
   	
   else if (u(6) /= 0 .AND. met1(1) /= 0) then
     g=6
   eps = 0.000001
   call bagidua (eps,number1,number2,res,count,4)
   if (maxcount > count) then
    write (10,*) 'Jawaban adalah ',res
   else
    write (10,*) 'Max count reached'
   end if
   
   else if (u(6) /= 0 .AND. met1(2) /= 0) then
     g=6
   eps = 0.000001
   call regulafalsi (eps,number1,number2,res,count,4)
   if (maxcount > count) then
    write (10,*) 'Jawaban adalah ',res
   else
    write (10,*) 'Max count reached'
   end if
   
   else if (u(6) /= 0 .AND. met1(3) /= 0 ) then
     g=6
   eps = 0.000001
   call secant (eps,number1,number2,res,count,4)
   if (maxcount > count) then
    write (10,*) 'Jawaban adalah ',res
   else
    write (10,*) 'Max count reached'
   end if
   
   else if (u(7) /= 0 .AND. met1(1) /= 0) then
     g=7
   eps = 0.000001
   call bagidua (eps,number1,number2,res,count,5)
   if (maxcount > count) then
    write (10,*) 'Jawaban adalah ',res
   else
    write (10,*) 'Max count reached'
   end if
   
   else if (u(7) /= 0 .AND. met1(2) /= 0) then
     g=7
   eps = 0.000001
   call regulafalsi (eps,number1,number2,res,count,5)
   if (maxcount > count) then
    write (10,*) 'Jawaban adalah ',res
   else
    write (10,*) 'Max count reached'
   end if
   
   else if (u(7) /= 0 .AND. met1(3) /= 0 ) then
     g=7
   eps = 0.000001
   call secant (eps,number1,number2,res,count,5)
   if (maxcount > count) then
    write (10,*) 'Jawaban adalah ',res
   else
    write (10,*) 'Max count reached'
   end if
   
   else if (u(8) /= 0 .AND. met1(1) /= 0 ) then
     g=8
   eps = 0.000001
   call bagidua (eps,number1,number2,res,count,6)
   if (maxcount > count) then
    write (10,*) 'Jawaban adalah ',res
   else
    write (10,*) 'Max count reached'
   end if
   
   
   else if (u(8) /= 0 .AND. met1(2) /= 0) then
     g=8
   eps = 0.000001
   call regulafalsi (eps,number1,number2,res,count,6)
   if (maxcount > count) then
    write (10,*) 'Jawaban adalah ',res
   else
    write (10,*) 'Max count reached'
   end if
   
   else if (u(8) /= 0 .AND. met1(3) /= 0 ) then
     g=8
   eps = 0.000001
   call secant (eps,number1,number2,res,count,6)
   if (maxcount > count) then
    write (10,*) 'Jawaban adalah ',res
   else
    write (10,*) 'Max count reached'
   end if

   else if (u(9)/=0 .AND. met1(1)/=0) then
     g=9
    eps = 0.000001
   call bagidua (eps,number1,number2,res,count,7)
   if (maxcount > count) then
    write (10,*) 'Jawaban adalah ',res
   else
    write (10,*) 'Max count reached'
   end if
   
   else if (u(9)/=0 .AND. met1(2)/=0) then
     g=9
    eps = 0.000001
   call regulafalsi (eps,number1,number2,res,count,7)
   if (maxcount > count) then
    write (10,*) 'Jawaban adalah ',res
   else
    write (10,*) 'Max count reached'
   end if
   
   else if (u(9)/=0 .AND. met1(3)/=0) then
     g=9
   eps = 0.000001
   call secant (eps,number1,number2,res,count,7)
   if (maxcount > count) then
    write (10,*) 'Jawaban adalah ',res
   else
    write (10,*) 'Max count reached'
   end if
   
   end if
   close(10)
   call read_data_file();
   if(g /= 0) then
	   call drawgraph(g)
   end if
   solve = -1
   end function solve
   
end aps_top 